package io.renren.modules.course.controller;

import java.util.*;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.oss.entity.MysqlOssEntity;
import io.renren.modules.oss.entity.SysOssEntity;
import io.renren.modules.oss.service.SysOssService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.course.entity.TeacherEntity;
import io.renren.modules.course.service.TeacherService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.exception.RRException;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 讲师表
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2018-07-12 15:30:24
 */
@RestController
@RequestMapping("course/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SysOssService sysOssService;
    @Autowired
    private SysUserService sysUserService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("course:teacher:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = teacherService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/getUserList")
    @RequiresPermissions("course:teacher:list")
    public R getUserList(@RequestParam Map<String, Object> params){
        params.put("page","1");
        params.put("limit","1000");
        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("course:teacher:info")
    public R info(@PathVariable("id") Integer id){
        TeacherEntity teacher = teacherService.selectById(id);

        return R.ok().put("teacher", teacher);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("course:teacher:save")
    public R save(@RequestBody TeacherEntity teacher){
        teacherService.insert(teacher);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("course:teacher:update")
    public R update(@RequestBody TeacherEntity teacher){
        ValidatorUtils.validateEntity(teacher);
        //teacherService.updateAllColumnById(teacher);//全部更新
        teacherService.updateTeacherInfo(teacher);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("course:teacher:delete")
    public R delete(@RequestBody Integer[] ids){
        teacherService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 上传文件
     */
    @RequestMapping("/upload")
                public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        //保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.insert(ossEntity);

        return R.ok().put("returnUrl", "http://"+url);

    }

    /**
     * 读取文件
     */
    @RequestMapping("/getimage/{name}")
    public @ResponseBody
    Map<String,Object>  getimage(@PathVariable("name")String name, HttpServletResponse response) throws Exception {
        MysqlOssEntity m=teacherService.getImgByName(name);
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + m.getName());
        ServletOutputStream toClient=response.getOutputStream(); //得到向客户端输出二进制数据的对象
        toClient.write(m.getImg());  //输出数据
        toClient.flush();
        toClient.close();
        return null;
    }

    /**
     * 删除
     */
    @RequestMapping("/getTeacherList")
    //@RequiresPermissions("course:teacher:delete")
    public R getTeacherList(){
       List list= teacherService.getTeacherList();

        return R.ok().put("teachers",list);
    }



}
